<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">






  
  
    
      
    
    
      
    
  <script src="https://cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
  <link href="https://cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-flash.min.css" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















  

<link href="https://cdn.bootcss.com/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.3.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.3.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.3.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.3.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.3.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '6.3.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="react使用指南使用 react 有段时间了，总感觉用的不够深入，连最基本异步处理方案 redux-saga 也才是前端时间刚学的。鉴于此，在 github 上搜了下相关的 react 项目，找到了一个外国人写的一个项目，看了内部 react 以及一些库的使用，整个 react 生态用的很不错，很多地方我都没有接触过，所以对一些写法和库的使用上做一些记录和总结。 相关类库由于查看的那个项目是 2">
<meta name="keywords" content="react">
<meta property="og:type" content="article">
<meta property="og:title" content="react使用指南">
<meta property="og:url" content="http://yoursite.com/2018/09/12/react使用指南/index.html">
<meta property="og:site_name" content="三月的米店">
<meta property="og:description" content="react使用指南使用 react 有段时间了，总感觉用的不够深入，连最基本异步处理方案 redux-saga 也才是前端时间刚学的。鉴于此，在 github 上搜了下相关的 react 项目，找到了一个外国人写的一个项目，看了内部 react 以及一些库的使用，整个 react 生态用的很不错，很多地方我都没有接触过，所以对一些写法和库的使用上做一些记录和总结。 相关类库由于查看的那个项目是 2">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://yoursite.com/2018/09/12/react使用指南/1755523965.jpg">
<meta property="og:updated_time" content="2018-11-29T02:09:38.099Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="react使用指南">
<meta name="twitter:description" content="react使用指南使用 react 有段时间了，总感觉用的不够深入，连最基本异步处理方案 redux-saga 也才是前端时间刚学的。鉴于此，在 github 上搜了下相关的 react 项目，找到了一个外国人写的一个项目，看了内部 react 以及一些库的使用，整个 react 生态用的很不错，很多地方我都没有接触过，所以对一些写法和库的使用上做一些记录和总结。 相关类库由于查看的那个项目是 2">
<meta name="twitter:image" content="http://yoursite.com/2018/09/12/react使用指南/1755523965.jpg">






  <link rel="canonical" href="http://yoursite.com/2018/09/12/react使用指南/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>react使用指南 | 三月的米店</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">三月的米店</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">今儿不吃饭</h1>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档</a>
  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/09/12/react使用指南/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Mark Lee">
      <meta itemprop="description" content="折腾小久的博客,在 2018-08-01 终于基本成型啦，以后就常驻啦">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="三月的米店">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">react使用指南
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-09-12 13:37:15" itemprop="dateCreated datePublished" datetime="2018-09-12T13:37:15+08:00">2018-09-12</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2018-11-29 10:09:38" itemprop="dateModified" datetime="2018-11-29T10:09:38+08:00">2018-11-29</time>
              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/前端/" itemprop="url" rel="index"><span itemprop="name">前端</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="react使用指南"><a href="#react使用指南" class="headerlink" title="react使用指南"></a>react使用指南</h1><p>使用 <code>react</code> 有段时间了，总感觉用的不够深入，连最基本异步处理方案 <code>redux-saga</code> 也才是前端时间刚学的。鉴于此，在 <code>github</code> 上搜了下相关的 <code>react</code> 项目，找到了一个外国人写的<a href="https://github.com/r-park/soundcloud-redux" target="_blank" rel="noopener">一个项目</a>，看了内部 <code>react</code> 以及一些库的使用，整个 <code>react</code> 生态用的很不错，很多地方我都没有接触过，所以对一些写法和库的使用上做一些记录和总结。</p>
<h2 id="相关类库"><a href="#相关类库" class="headerlink" title="相关类库"></a>相关类库</h2><p>由于查看的那个项目是 <em>2017年</em> 写的，所以有一些库不太一样了，这里我结合自身的使用情况总结下一个还算完整的 <code>react</code> 项目可能会用到库：</p>
<table>
<thead>
<tr>
<th>库名</th>
<th>用途</th>
<th>类似功能的库</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>react</code></td>
<td>核心库</td>
<td>/</td>
</tr>
<tr>
<td><code>react-dom</code></td>
<td>核心库</td>
<td>/</td>
</tr>
<tr>
<td><code>prop-types</code></td>
<td>props校验库</td>
<td>/</td>
</tr>
<tr>
<td><code>react-router-dom</code></td>
<td>路由库</td>
<td><code>reach/router</code></td>
</tr>
<tr>
<td><code>redux</code></td>
<td>状态管理库</td>
<td><code>Mobx</code>、<code>rematch</code></td>
</tr>
<tr>
<td><code>react-redux</code></td>
<td>连接 <code>react</code>、<code>redux</code></td>
<td>/</td>
</tr>
<tr>
<td><code>redux-saga</code></td>
<td><code>redux</code> 中间件，解决异步问题</td>
<td><code>redux-thunk</code>、<code>redux-promise</code></td>
</tr>
<tr>
<td><code>redux-devtools-extension</code></td>
<td><code>chrome</code> 的 <code>redux</code> 调试工具</td>
<td>/</td>
</tr>
<tr>
<td><code>reselect</code></td>
<td><code>store</code> 上取值能够缓存</td>
<td>/</td>
</tr>
<tr>
<td><code>immutable</code></td>
<td>不可变数据</td>
<td>/</td>
</tr>
</tbody>
</table>
<p>由此可见，<code>react</code> 全家桶一次性学习下来，还是有一定的门槛的，接下来汇总下基本使用套路。</p>
<h2 id="使用套路"><a href="#使用套路" class="headerlink" title="使用套路"></a>使用套路</h2><p>老实说，<code>react</code> 是一个学习、使用相当平滑的库，所以简单的使用还是比较容易的，主要学习的难点还是在 <code>redux</code> 以及像 <code>immutable</code> 这样的很少用的库。之前，我是没有用过<code>immutable</code> 和 <code>reselect</code> ，这里就对着别人项目记录下。</p>
<h3 id="redux初始化"><a href="#redux初始化" class="headerlink" title="redux初始化"></a>redux初始化</h3><p><code>redux</code> 本身是一个很纯粹的状态管理库，和 <code>react</code> 本身没有任何瓜葛，但是用 <code>react-redux</code> 可以把 <code>react</code> 和 <code>redux</code> 结合起来。具体细节 <code>api</code> 不谈，直接记录平时如何使用：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line"><span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">'react-dom'</span></span><br><span class="line"><span class="keyword">import</span> &#123; createStore, applyMiddleware &#125; <span class="keyword">from</span> <span class="string">'redux'</span></span><br><span class="line"><span class="keyword">import</span> &#123;Provider&#125; <span class="keyword">from</span> <span class="string">'react-redux'</span></span><br><span class="line"><span class="keyword">import</span> createSagaMiddleware <span class="keyword">from</span> <span class="string">'redux-saga'</span></span><br><span class="line"><span class="keyword">import</span> &#123;composeWithDevTools&#125; <span class="keyword">from</span> <span class="string">'redux-devtools-extension'</span></span><br><span class="line"><span class="keyword">import</span> reducer <span class="keyword">from</span> <span class="string">'./store/reducers'</span></span><br><span class="line"><span class="keyword">import</span> rootSaga <span class="keyword">from</span> <span class="string">'./store/sagas/index'</span></span><br><span class="line"><span class="keyword">import</span> &#123; AppWithRouter &#125; <span class="keyword">from</span> <span class="string">'./router/router'</span></span><br><span class="line"><span class="keyword">const</span> sagaMiddleware = createSagaMiddleware()</span><br><span class="line"><span class="keyword">const</span> composeEnhancers = composeWithDevTools(&#123;&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> store = createStore(</span><br><span class="line">  reducer,</span><br><span class="line">  composeEnhancers(</span><br><span class="line">      applyMiddleware(sagaMiddleware)</span><br><span class="line">  ),</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">sagaMiddleware.run(rootSaga)</span><br><span class="line"></span><br><span class="line">ReactDOM.render(</span><br><span class="line">    &lt;Provider store=&#123;store&#125;&gt;</span><br><span class="line">      &lt;AppWithRouter /&gt;</span><br><span class="line">    &lt;<span class="regexp">/Provider&gt;,</span></span><br><span class="line"><span class="regexp">    document.getElementById('root')</span></span><br><span class="line"><span class="regexp">  )</span></span><br></pre></td></tr></table></figure>
<p>这里根据 <code>reducer</code> 生成了 <code>store</code>，把 <code>store</code> 挂载到 <code>Provider</code> 上面去了，后面的子组件就会根据 <code>context</code> 去拿到 <code>store</code> 上的值。</p>
<p>这里的 <code>AppWithRouter</code> 是我想要渲染的组件，<code>reducer</code> 、<code>rootSaga</code> 是我业务相关的内容，而其他内容可以发现，基本都是固定的，下一个项目基本可以照搬过来。</p>
<h3 id="reducer写法"><a href="#reducer写法" class="headerlink" title="reducer写法"></a>reducer写法</h3><p>这里先看我的很一般的 <code>reducer</code> 写法，再看一下别人结合 <code>immutable</code> 的 <code>reducer</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 我的reducer写法</span></span><br><span class="line"><span class="keyword">import</span> &#123;actionTypes&#125; <span class="keyword">from</span> <span class="string">'../action-type'</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">pageList</span>(<span class="params">state = &#123;</span></span></span><br><span class="line"><span class="function"><span class="params">    list: [],</span></span></span><br><span class="line"><span class="function"><span class="params">    isLoading: false</span></span></span><br><span class="line"><span class="function"><span class="params">&#125;, action</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">switch</span> (action.type) &#123;</span><br><span class="line">        <span class="keyword">case</span> actionTypes.FETCH_LIST_SUCCESS:</span><br><span class="line">            <span class="keyword">return</span> &#123;</span><br><span class="line">                ...state,</span><br><span class="line">                list: action.payload</span><br><span class="line">            &#125;</span><br><span class="line">        <span class="keyword">case</span> actionTypes.LIST_LOADING:</span><br><span class="line">            <span class="keyword">return</span> &#123;</span><br><span class="line">                ...state,</span><br><span class="line">                isLoading: action.payload</span><br><span class="line">            &#125;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">return</span> state</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// action-type.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> actionTypes = &#123;</span><br><span class="line">    <span class="comment">// 详情页面</span></span><br><span class="line">    FETCH_DETAIL: <span class="string">'FETCH_DETAIL'</span>,</span><br><span class="line">    FETCH_DETAIL_SUCCESS: <span class="string">'FETCH_DETAIL_SUCCESS'</span>,</span><br><span class="line">    DETAIL_LOADING: <span class="string">'DETAIL_LOADING'</span>,</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 列表页面</span></span><br><span class="line">    FETCH_LIST_SUCCESS: <span class="string">'FETCH_LIST_SUCCESS'</span>,</span><br><span class="line">    LIST_LOADING: <span class="string">'LIST_LOADING'</span>,</span><br><span class="line">    FETCH_LIST: <span class="string">'FETCH_LIST'</span>,</span><br><span class="line"></span><br><span class="line">    <span class="comment">// tab</span></span><br><span class="line">    CHANGE_TAB: <span class="string">'CHANGE_TAB'</span>,</span><br><span class="line"></span><br><span class="line">    <span class="comment">// currentPage</span></span><br><span class="line">    CHANGE_PAGE: <span class="string">'CHANGE_PAGE'</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>只要注意把固定的字符串全部写成变量。</p>
<p>由于 <code>redux</code> 需要保持纯函数的特点，所以 <code>redux</code> 是不能直接修改 <code>state</code> 的值，应该返回一个全新的 <code>state</code> ，所以如果 <code>state</code> 的嵌套层数很深的话，要返回全新的 <code>state</code> 就比较麻烦了，所以这里就引申出来 <code>immutable</code>，同样在组件 <code>shouldComponentUpdate</code> 时需要对比两个对象时，<code>immutable</code> 也能帮上很大的忙。</p>
<p>看看别人 <code>reducer</code> 的用法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; Record &#125; <span class="keyword">from</span> <span class="string">'immutable'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; searchActions &#125; <span class="keyword">from</span> <span class="string">'./actions'</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> SearchState = <span class="keyword">new</span> Record(&#123;</span><br><span class="line">  currentQuery: <span class="literal">null</span>,</span><br><span class="line">  open: <span class="literal">false</span></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">searchReducer</span>(<span class="params">state = new SearchState(</span>), </span>&#123;payload, type&#125;) &#123;</span><br><span class="line">  <span class="keyword">switch</span> (type) &#123;</span><br><span class="line">    <span class="keyword">case</span> searchActions.LOAD_SEARCH_RESULTS:</span><br><span class="line">      <span class="keyword">return</span> state.merge(&#123;</span><br><span class="line">        open: <span class="literal">false</span>,</span><br><span class="line">        currentQuery: payload.query</span><br><span class="line">      &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">case</span> searchActions.TOGGLE_SEARCH_FIELD:</span><br><span class="line">      <span class="keyword">return</span> state.set(<span class="string">'open'</span>, !state.open);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">      <span class="keyword">return</span> state;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>相比我的直接对象，这里用了 <code>immutable</code> 的 <code>Record</code>，在 <code>reducer</code> 内部需要修改 <code>state</code> 的时候，直接调用 <code>set</code> 方法就去修改了，在层级很深的对象的时候是非常方便的。</p>
<h3 id="saga的写法"><a href="#saga的写法" class="headerlink" title="saga的写法"></a>saga的写法</h3><p>当初有点恐惧学习 <code>redux-saga</code>，实际去学习和使用的时候发现还是很不错的，相比<code>redux-thunk</code> 去强行让 <code>action</code> 能够是个函数，<code>redux-saga</code> 还是保持 <code>action</code> 是一个对象，脏活累活全丢给 <code>saga</code> 去做，<code>redux</code> 的那一块逻辑依然保持之前一样纯净。先上例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; call, fork, select, take, takeLatest &#125; <span class="keyword">from</span> <span class="string">'redux-saga/effects'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; fetchSearchResults &#125; <span class="keyword">from</span> <span class="string">'src/core/api'</span>;</span><br><span class="line"><span class="keyword">import</span> history <span class="keyword">from</span> <span class="string">'src/core/history'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; getTracklistById &#125; <span class="keyword">from</span> <span class="string">'src/core/tracklists'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; searchActions &#125; <span class="keyword">from</span> <span class="string">'./actions'</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span>* <span class="title">loadSearchResults</span>(<span class="params">&#123;payload&#125;</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> &#123; query, tracklistId &#125; = payload;</span><br><span class="line">  <span class="keyword">const</span> tracklist = <span class="keyword">yield</span> select(getTracklistById, tracklistId);</span><br><span class="line">  <span class="keyword">if</span> (tracklist &amp;&amp; tracklist.isNew) &#123;</span><br><span class="line">    <span class="keyword">yield</span> call(fetchSearchResults, tracklistId, query);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//=====================================</span></span><br><span class="line"><span class="comment">//  WATCHERS</span></span><br><span class="line"><span class="comment">//-------------------------------------</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span>* <span class="title">watchLoadSearchResults</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">yield</span> takeLatest(searchActions.LOAD_SEARCH_RESULTS, loadSearchResults);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span>* <span class="title">watchNavigateToSearch</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; payload &#125; = <span class="keyword">yield</span> take(searchActions.NAVIGATE_TO_SEARCH);</span><br><span class="line">    <span class="keyword">yield</span> history.push(payload);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//=====================================</span></span><br><span class="line"><span class="comment">//  ROOT</span></span><br><span class="line"><span class="comment">//-------------------------------------</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> searchSagas = [</span><br><span class="line">  fork(watchLoadSearchResults),</span><br><span class="line">  fork(watchNavigateToSearch)</span><br><span class="line">];</span><br></pre></td></tr></table></figure>
<p>这玩意按我目前的理解，saga分两块，一块专门用来 <code>watch</code>，一块是处理，<code>watch</code> 用<code>while</code> 死循环 <code>take</code> 或者<code>takeEvery</code>、<code>takeLatest</code> 去 <code>watch</code> 对应的 <code>action/type</code>， 然后调用另一个 <code>sagas</code> ，在另一个 <code>sagas</code> 用 <code>call</code> 之类的去调用异步的 <code>api/service</code>。</p>
<h3 id="store和视图"><a href="#store和视图" class="headerlink" title="store和视图"></a>store和视图</h3><p>扯了半天 <code>redux</code>，看最后是怎么把 <code>redux</code> 上的store数据关联到视图层上，以及视图如何去改变store里面的值，主要还是 <code>react-redux</code> 用 <code>connect</code> 把 <code>store</code> 的数据以及 <code>dispatch</code> 给组件，这样组件就能获取数据以及修改数据了。</p>
<p>先看我的常规做法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line"><span class="keyword">import</span> &#123;compose&#125; <span class="keyword">from</span> <span class="string">'redux'</span></span><br><span class="line"><span class="keyword">import</span> &#123;withRouter&#125; <span class="keyword">from</span> <span class="string">'react-router-dom'</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">'react-redux'</span></span><br><span class="line"><span class="keyword">import</span> &#123;actionTypes&#125; <span class="keyword">from</span> <span class="string">'../store/action-type'</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">DetailPage</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">    componentDidMount () &#123;</span><br><span class="line">        <span class="keyword">const</span> &#123;fetchDetail&#125; = <span class="keyword">this</span>.props</span><br><span class="line">        fetchDetail()</span><br><span class="line">    &#125;</span><br><span class="line">    render () &#123;</span><br><span class="line">        <span class="keyword">const</span> &#123;detail, isLoading&#125; = <span class="keyword">this</span>.props</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">           xxx</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> mapStateToProps = <span class="function"><span class="params">state</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        detail: state.detailData.data,</span><br><span class="line">        isLoading: state.detailData.isLoading</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> mapDispatchToProps = <span class="function">(<span class="params">dispatch, ownProps</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        fetchDetail() &#123;</span><br><span class="line">            dispatch(&#123;</span><br><span class="line">                type: actionTypes.FETCH_DETAIL,</span><br><span class="line">                payload: ownProps.match.params.id</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> compose(</span><br><span class="line">    withRouter,</span><br><span class="line">    connect(mapStateToProps, mapDispatchToProps),</span><br><span class="line">)(DetailPage)</span><br></pre></td></tr></table></figure>
<p>还是很简单的，直接在 <code>connect</code> 中传两个参数 <code>mapStateToProps</code>、<code>mapDispatchToProps</code> 过去就完事了，这样组件需要什么值，需要什么方法都能提供。</p>
<p>顺带一提，用上 <code>withRouter</code>，这样路由信息也能给到组件。</p>
<p>看下用了 <code>reselect</code> 之后，是怎么用的：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; connect &#125; <span class="keyword">from</span> <span class="string">'react-redux'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; createSelector &#125; <span class="keyword">from</span> <span class="string">'reselect'</span>;</span><br><span class="line"><span class="keyword">import</span> classNames <span class="keyword">from</span> <span class="string">'classnames'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; List &#125; <span class="keyword">from</span> <span class="string">'immutable'</span>;</span><br><span class="line"><span class="keyword">import</span> PropTypes <span class="keyword">from</span> <span class="string">'prop-types'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; getBrowserMedia, infiniteScroll &#125; <span class="keyword">from</span> <span class="string">'src/core/browser'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; audio, getPlayerIsPlaying, getPlayerTrackId, playerActions &#125; <span class="keyword">from</span> <span class="string">'src/core/player'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; getCurrentTracklist, getTracksForCurrentTracklist, tracklistActions &#125; <span class="keyword">from</span> <span class="string">'src/core/tracklists'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">Tracklist</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">  <span class="keyword">static</span> propTypes = &#123;</span><br><span class="line">    compactLayout: PropTypes.bool,</span><br><span class="line">    displayLoadingIndicator: PropTypes.bool.isRequired,</span><br><span class="line">    isMediaLarge: PropTypes.bool.isRequired,</span><br><span class="line">    isPlaying: PropTypes.bool.isRequired,</span><br><span class="line">    loadNextTracks: PropTypes.func.isRequired,</span><br><span class="line">    pause: PropTypes.func.isRequired,</span><br><span class="line">    pauseInfiniteScroll: PropTypes.bool.isRequired,</span><br><span class="line">    play: PropTypes.func.isRequired,</span><br><span class="line">    selectTrack: PropTypes.func.isRequired,</span><br><span class="line">    selectedTrackId: PropTypes.number,</span><br><span class="line">    tracklistId: PropTypes.string.isRequired,</span><br><span class="line">    tracks: PropTypes.instanceOf(List).isRequired</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  componentDidMount() &#123;</span><br><span class="line">    infiniteScroll.start(</span><br><span class="line">      <span class="keyword">this</span>.props.loadNextTracks,</span><br><span class="line">      <span class="keyword">this</span>.props.pauseInfiniteScroll</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  componentWillUpdate(nextProps) &#123;</span><br><span class="line">    <span class="keyword">if</span> (nextProps.pauseInfiniteScroll !== <span class="keyword">this</span>.props.pauseInfiniteScroll) &#123;</span><br><span class="line">      <span class="keyword">if</span> (nextProps.pauseInfiniteScroll) &#123;</span><br><span class="line">        infiniteScroll.pause();</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">else</span> &#123;</span><br><span class="line">        infiniteScroll.resume();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  componentWillUnmount() &#123;</span><br><span class="line">    infiniteScroll.end();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  render() &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; compactLayout, isMediaLarge, isPlaying, pause, play, selectedTrackId, selectTrack, tracklistId, tracks &#125; = <span class="keyword">this</span>.props;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      xxxx</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//=====================================</span></span><br><span class="line"><span class="comment">//  CONNECT</span></span><br><span class="line"><span class="comment">//-------------------------------------</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> mapStateToProps = createSelector(</span><br><span class="line">  getBrowserMedia,</span><br><span class="line">  getPlayerIsPlaying,</span><br><span class="line">  getPlayerTrackId,</span><br><span class="line">  getCurrentTracklist,</span><br><span class="line">  getTracksForCurrentTracklist,</span><br><span class="line">  (media, isPlaying, playerTrackId, tracklist, tracks) =&gt; (&#123;</span><br><span class="line">    displayLoadingIndicator: tracklist.isPending || tracklist.hasNextPage,</span><br><span class="line">    isMediaLarge: !!media.large,</span><br><span class="line">    isPlaying,</span><br><span class="line">    pause: audio.pause,</span><br><span class="line">    pauseInfiniteScroll: tracklist.isPending || !tracklist.hasNextPage,</span><br><span class="line">    play: audio.play,</span><br><span class="line">    selectedTrackId: playerTrackId,</span><br><span class="line">    tracklistId: tracklist.id,</span><br><span class="line">    tracks</span><br><span class="line">  &#125;)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> mapDispatchToProps = &#123;</span><br><span class="line">  loadNextTracks: tracklistActions.loadNextTracks,</span><br><span class="line">  selectTrack: playerActions.playSelectedTrack</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(</span><br><span class="line">  mapStateToProps,</span><br><span class="line">  mapDispatchToProps</span><br><span class="line">)(Tracklist);</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// selector.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">getBrowserMedia</span>(<span class="params">state</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> state.browser.media;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>具体关注下用了 <code>reselect</code> 之后，<code>mapStateToProps</code> 和我之前的写法发生了变化，正如给的例子那样用 <code>createSelector</code> 包了一层，同时传入两个参数进去，第一个参数是个从 <code>state</code> 上取值的函数，就像上面的 <code>getBrowserMedia</code> 这个例子一样。至于 <code>mapDispatchToProps</code> 的写法，在我的用法是写一个接受 <code>dispatch</code> 的函数同时返回一个对象，当然也可以像上面一样传入一个对象，这个对象 <code>redux</code> 就默认做为 <code>action</code>。</p>
<h3 id="props-验证"><a href="#props-验证" class="headerlink" title="props 验证"></a>props 验证</h3><p>上面介绍了那么多 <code>redux</code> 相关写法，<code>redux</code> 确实算是 <code>react</code> 学习上的一个难点，现在讲点轻松点的。<code>redux</code> 推崇<strong>容器组件</strong>和<strong>展示组件</strong>，实际上在写 <code>react</code> 应用的时候，你也可能不太会注意到，其实用 <code>connect</code> 这个高阶函数包装过的组件就是所谓的<strong>容器组件</strong>，而传给 <code>connect</code> 的组件，其实就是我们写的<strong>展示组件</strong>，写的多了就会发现哈，我们越来越少地用到了组件内部的 <code>state</code> 去控制组件，反而大部分情况都是直接用 <code>props</code> 去控制组件，这也得益于 <code>redux</code> 能够提供类似全局变量 <code>store</code> 的取值和改变值的方式。所以说回来，对于一个 <code>react</code> 组件而言，<code>state</code> 对应内部状态，<code>props</code> 对应外部传入值，<code>props</code> 由于 <code>redux</code> 等状态管理库盛行，使用频率也大幅增加，所以我们需要严格要求好外部传入的 <code>props</code>的类型要符合组件规定的。<code>prop-types</code> 就是解决这个问题的，当然你也可以不去校验 <code>props</code> 的类型。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> propTypes = &#123;</span><br><span class="line">        compactLayout: PropTypes.bool,</span><br><span class="line">        displayLoadingIndicator: PropTypes.bool.isRequired,</span><br><span class="line">        isMediaLarge: PropTypes.bool.isRequired,</span><br><span class="line">        isPlaying: PropTypes.bool.isRequired,</span><br><span class="line">        loadNextTracks: PropTypes.func.isRequired,</span><br><span class="line">        pause: PropTypes.func.isRequired,</span><br><span class="line">        pauseInfiniteScroll: PropTypes.bool.isRequired,</span><br><span class="line">        play: PropTypes.func.isRequired,</span><br><span class="line">        selectTrack: PropTypes.func.isRequired,</span><br><span class="line">        selectedTrackId: PropTypes.number,</span><br><span class="line">        tracklistId: PropTypes.string.isRequired,</span><br><span class="line">        tracks: PropTypes.instanceOf(List).isRequired</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">static</span> defaultProps = &#123;</span><br><span class="line">        compactLayout: <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">    render () &#123;</span><br><span class="line">        <span class="keyword">return</span> xxx</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p><code>react</code> 本身不难，甚至我觉得比起 <code>vue</code> 而言更为简单，使用难点主要还是在于一些第三方库的搭配使用，所以本文也是基于这个点，记录下一些 <code>react</code> 常见用法，以便日后忘记了可以翻阅。</p>
<hr>
<p><img src="/2018/09/12/react使用指南/1755523965.jpg" alt="Olive Trees"></p>
<blockquote>
<p>Vincent van Gogh – Olive Trees  1888</p>
</blockquote>

      
    </div>

    

    
    
    

    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/react/" rel="tag"><i class="fa fa-tag"></i> react</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/09/03/redux之applyMiddleware/" rel="next" title="redux之applyMiddleware">
                <i class="fa fa-chevron-left"></i> redux之applyMiddleware
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/12/02/阴天/" rel="prev" title="阴天">
                阴天 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Mark Lee</p>
              <p class="site-description motion-element" itemprop="description">折腾小久的博客,在 2018-08-01 终于基本成型啦，以后就常驻啦</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">26</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">4</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">20</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#react使用指南"><span class="nav-number">1.</span> <span class="nav-text">react使用指南</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#相关类库"><span class="nav-number">1.1.</span> <span class="nav-text">相关类库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用套路"><span class="nav-number">1.2.</span> <span class="nav-text">使用套路</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#redux初始化"><span class="nav-number">1.2.1.</span> <span class="nav-text">redux初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#reducer写法"><span class="nav-number">1.2.2.</span> <span class="nav-text">reducer写法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#saga的写法"><span class="nav-number">1.2.3.</span> <span class="nav-text">saga的写法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#store和视图"><span class="nav-number">1.2.4.</span> <span class="nav-text">store和视图</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#props-验证"><span class="nav-number">1.2.5.</span> <span class="nav-text">props 验证</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">1.3.</span> <span class="nav-text">总结</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Mark Lee</span>

  

  
</div>


  


<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" rel="external nofollow" href="https://hexo.io">Hexo</a> 强力驱动 v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" rel="external nofollow" href="https://theme-next.org">NexT.Pisces</a> v6.3.0</div>

-->



        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
	
    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.3.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.3.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.3.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.3.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.3.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.3.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.3.0"></script>



  



  










  





  

  

  

  

  
  

  

  

  

  

  

</body>
</html>
